package com.stu.maphandler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhuzi
 * 2022/5/31
 */
@Slf4j
@Component
public class SelectChooser implements ApplicationContextAware {


	private final Map<String, OperateHandler> map = new HashMap<>();
	private ApplicationContext context;

	public OperateHandler select(String sel) {
		return map.get(sel);
	}

	@PostConstruct
	public void register() {
		Map<String, OperateHandler> solverMap = context.getBeansOfType(OperateHandler.class);
		for (OperateHandler handler : solverMap.values()) {
			for (String support : handler.support()) {
				map.put(support, handler);
			}
		}
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}
}
